home *** CD-ROM | disk | FTP | other *** search
/ PC World Interactive 9 / PC World Interactive 9 - Temmuz 1998.iso / share / internet / hot / cgi.z / html_log.pl < prev    next >
Perl Script  |  1996-04-17  |  8KB  |  278 lines

  1. #! /usr/local/bin/perl
  2.  
  3. # Access Stat HTML Generator
  4. # Used in conjunction with Counter Script
  5. # Created by Matt Wright    mattw@misha.net
  6. # Created on: 10/25/95          Last Modified on: 10/26/95
  7. # Version 1.0
  8. # The file STAT_README contains more information.
  9.  
  10. ###################################################################
  11. # Define Variables
  12.  
  13. $log_file = "/path/to/access_log";
  14.  
  15. $web = "1";
  16.  
  17. $min_refs = "5";
  18. $min_remote = "15"; 
  19. $min_agent = "5";
  20.  
  21. # Done
  22. ###################################################################
  23.  
  24. ###################################################################
  25. # Select Options
  26.  
  27. $expand_agent = 0;      # 0 = NO; 1 = YES
  28. $show_percent = 1;      # 0 = NO; 1 = YES
  29.  
  30. $title = "Matt's Script Archive";
  31. $title_url = "http://worldwidemart.com/scripts/";
  32.  
  33. # Done
  34. ###################################################################
  35.  
  36. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  37. if ($sec < 10)  { $sec = "0$sec";   }
  38. if ($min < 10)  { $min = "0$min";   }
  39. if ($hour < 10) { $hour = "0$hour"; }
  40. if ($mday < 10) { $mday = "0$mday"; }
  41. if ($mon < 10)  { $monc = "0$mon";  }
  42.  
  43. if ($web == 1) {
  44.    print "Content-type: text/html\n\n";
  45. }
  46.  
  47. $date_now = "$hour\:$min\:$sec $mon/$mday/$year";
  48.  
  49. open(DB,"$log_file") || die "Cannot Open Log File $log_file: $!";
  50. @lines = <DB>;
  51. close(DB);
  52.  
  53. $accesses = @lines;
  54.  
  55. if ($lines[1] =~ /\[(.*)\] (.*) - (.*) - (.*)/) {
  56.    $first_date = $1;
  57. }
  58. else {
  59.    $first_date = 0;
  60. }
  61.  
  62. if ($lines[($accesses - 1)] =~ /\[(.*)\] (.*) - (.*) - (.*)/) {
  63.    $last_date = $1;
  64. }
  65. else {
  66.    $last_date = 0;
  67. }
  68.  
  69. foreach $line (@lines) {
  70.    if ($line =~ /\[(.*)\] (.*) - (.*) - (.*)/) {
  71.       $date = $1;
  72.       ($clock,$time,$day) = split(/ /,$date);
  73.       ($hour,$minute,$second) = split(/:/,$clock);
  74.       $referer = $2;
  75.       $referer =~ s/\%24/\$/g;
  76.       $referer =~ s/\%7E/~/g;
  77.       $remote_host = $3;
  78.       $user_agent = $4;
  79.  
  80.       if ($time eq 'PM') {
  81.          $hour += 12;
  82.       }
  83.  
  84.       if ($day ne '' && $day ne ' ') {
  85.          push(@DAYS, $day);
  86.       }
  87.       if ($hour ne '' && $hour ne ' ') {
  88.          push(@HOURS, $hour);
  89.       }
  90.       if ($referer ne 'No Referer' && $referer ne ' ' && $referer ne '') {
  91.          push(@REFERER, $referer);
  92.       }
  93.       if ($remote_host ne 'No Remote_Host' && $remote_host ne ' ' && $remote_host ne '') {
  94.          push(@REMOTE_HOST, $remote_host);
  95.       }
  96.       if ($user_agent ne 'No User_Agent' && $user_agent ne ' ' && $user_agent ne '') {
  97.          push(@USER_AGENT, $user_agent);
  98.       }
  99.    }
  100. }
  101.  
  102. foreach (@REFERER) {
  103.    $refs{($_)[0]}++;
  104.    $i++;
  105. }
  106.  
  107. foreach (@REMOTE_HOST) {
  108.    $remote{($_)[0]}++;
  109.    $j++;
  110. }
  111.  
  112. foreach (@USER_AGENT) {
  113.    if ($expand_agent == 1) {
  114.       $agent{($_)[0]}++;
  115.    }
  116.    else {
  117.       $agent{(split('/',$_))[0]}++;
  118.    }
  119.    $k++;
  120. }
  121.  
  122. foreach (@DAYS) {
  123.    $day{($_)[0]}++;
  124. }
  125.  
  126. foreach (@HOURS) {
  127.    $hour{($_)[0]}++;
  128. }
  129.  
  130. &html_header;
  131.  
  132. sub html_header {
  133.    print "<html><head><title>Access Stats for $title</title></head>\n";
  134.    print "<body><center><h1>Access Stats for $title</h1></center>\n";
  135.    print "Below are the access stats for $title.<p>\n";
  136.    print "A total of $accesses were reviewed for this logging, which occurred at: $date_now<p>\n";
  137.    if ($first_date != 0 && $last_date != 0) {
  138.       print "These statistics reflect accesses from: <i>$first_date</i> to <i>$last_date</i><p>\n";
  139.    }
  140.    print "<p><hr size=7 width=75%>\n";
  141.    print "<font size=-1>[ <a href=\"\#refs\">Referring Web Pages</a> ] [ <a href=\"\#remote_host\">Remote Hosts</a> ] [ <a href=\"\#browsers\">Browsers</a> ] [ <a href=\"\#days\">Hits by Day</a> ] [ <a href=\"\#hours\">Hits by Hour</a> ] [ <a href=\"$titl
  142.  
  143.  
  144.  
  145. e_url\">$title</a> ]</font>\n";
  146.    print "<hr size=7 width=75%><p>\n";
  147.  
  148.    &html_referer;
  149.    &html_remote_host;
  150.    &html_user_agent;
  151.    &days;
  152.    &hours;
  153. }
  154.  
  155. sub html_referer {
  156.    print "<center><h2><a name=\"refs\">Referring Web Pages</a></h2></center>\n";
  157.    print "<b>Referring URLs Searched:</b> <i><u>$i</u></i><br>\n";
  158.    print "<b>Minimum References Required to Make List:</b> $min_refs</u></i><p>\n";
  159.  
  160.    print "<table border width=100%>\n";
  161.    if ($show_percent == 1) {
  162.       print "<tr><th>Number </th><th>Percent </th><th>Referring Web Sites<br></th></tr>\n";
  163.    }
  164.    else {
  165.       print "<tr><th>Number </th><th>Referring Web Sites<br></th></tr>\n";
  166.    }
  167.    foreach (sort { $refs{$b} <=> $refs{$a} } keys %refs) {
  168.       if ($refs{$_} >= $min_refs) {
  169.          print "<tr>\n";
  170.          $total_refs += $refs{$_};
  171.          if ($show_percent == 1) {
  172.             $percent_refs = (int(10000 * ($refs{$_} / $i)) / 100);
  173.             $total_percent_refs += $percent_refs;
  174.             print "<th>$refs{$_} </th><th>$percent_refs\% </th><td><a href=\"$_\">$_</a><br></td>\n";
  175.          }
  176.          else {
  177.             print "<th>$refs{$_} </th><td><a href=\"$_\">$_</a><br></td>\n";
  178.          }
  179.          print "</tr>\n";
  180.       }
  181.    }
  182.    if ($show_percent == 1) {
  183.       print "<tr><th>$total_refs </th><th>$total_percent_refs\% </th><th>Totals For URLS Shown<br></th></tr>\n";
  184.    }
  185.    else {
  186.       print "<tr><th>$total_refs </th><th>Totals For URLS Shown<br></th></tr>\n";
  187.    }
  188.    print "</table><hr size=7 width=75%><p>\n";
  189. }
  190.  
  191. sub html_remote_host {
  192.    print "<center><h2><a name=\"remote_host\">Remote Hosts</a></h2></center>\n";
  193.    print "<b>Remote Hosts Searched:</b> <u><i>$j</i></u><br>\n";
  194.    print "<b>Minimum Hits Required to Make List:</b> <i><u>$min_remote</u></i><p>\n";
  195.  
  196.    print "<table border>\n";
  197.    if ($show_percent == 1) {
  198.       print "<tr><th>Number of Hits </th><th>Percent </th><th>Remote Hosts<br></th></tr>\n";
  199.    }
  200.    else {
  201.       print "<tr><th>Number of Hits </th><th>Remote Hosts<br></th></tr>\n";
  202.    }
  203.    foreach (sort { $remote{$b} <=> $remote{$a} } keys %remote) {
  204.       if ($remote{$_} >= $min_remote) {
  205.          print "<tr>\n";
  206.          if ($show_percent == 1) {
  207.             $percent_remote = (int(10000 * ($remote{$_} / $j)) / 100);
  208.             print "<th>$remote{$_} </th><th>$percent_remote\% </th><td>$_<br></td>\n";
  209.          }
  210.          else {
  211.             print "<th>$remote{$_} </th><td>$_<br></td>\n";
  212.          }
  213.          print "</tr>\n";
  214.       }
  215.    }
  216.    print "</table><hr size=7 width=75%><p>\n";
  217. }
  218.  
  219. sub html_user_agent {
  220.    print "<a name=\"browsers\"><center><h2>WWW Browsers</h2></center></a>\n";
  221.    print "<b>Browsers Searched:</b> <u><i>$k</i></u><br>\n";
  222.    print "<b>Minimum Hits Required to Make List:</b> <u><i>$min_agent</i></u><p>\n";
  223.  
  224.    print "<table border>\n";
  225.    if ($show_percent == 1) {
  226.       print "<tr><th>Number of Hits </th><th>Percent </th><th>Browser<br></th></tr>\n";
  227.    }
  228.    else {
  229.       print "<tr><th>Number of Hits </th><th>Browser<br></th></tr>\n";
  230.    }
  231.  
  232.    foreach (sort { $agent{$b} <=> $agent{$a} } keys %agent) {
  233.       if ($agent{$_} >= $min_agent) {
  234.          print "<tr>\n";
  235.          if ($show_percent == 1) {
  236.             $percent_agent = (int(10000 * ($agent{$_} / $k)) / 100);
  237.             print "<th>$agent{$_} </th><th>$percent_agent\% </th><td>$_<br></td>\n";
  238.          }
  239.          else {
  240.             print "<th>$agent{$_} </th><td>$_<br></td>\n";
  241.          }
  242.          print "</tr>\n";
  243.       }
  244.    }
  245.    print "</table>\n";
  246. }
  247.  
  248. sub hours {
  249.    print "<a name=\"hours\"><center><h2>Hits By Hour</h2></center></a>\n";
  250.    print "<table border>\n";
  251.    print "<tr><th>Hour </th><td>Number of Hits<br></td></tr>\n";
  252.    foreach (sort keys %hour) {
  253.       print "<tr>\n";
  254.       print "<th>$_ </th><td>$hour{$_}<br></td>\n";
  255.       print "</tr>\n";
  256.    }
  257.    print "</table>\n";
  258. }
  259.  
  260. sub days {
  261.    print "<a name=\"days\"><center><h2>Hits By Day</h2></center></a>\n";
  262.    print "<table border>\n";
  263.    print "<tr><th>Day </th><td>Number of Hits<br></td></tr>\n";
  264.    foreach (sort keys %day) {
  265.       print "<tr>\n";
  266.       print "<th>$_ </th><td>$day{$_}<br></td>\n";
  267.       print "</tr>\n";
  268.    }
  269.    print "</table>\n";
  270. }
  271.  
  272. sub html_trailer {
  273.    print "<p><hr size=7 width=75%>\n";
  274.    print "<font size=-1>[ <a href=\"\#refs\">Referring Web Pages</a> ] [ <a href=\"\#remote_host\">Remote Hosts</a> ] [ <a href=\"\#browsers\">Browsers</a> ] [ <a href=\"$title_url\">$title</a> ]</font>\n";
  275.    print "<hr size=7 width=75%><p>\n";
  276.    print "</body></html>\n";
  277. }
  278.